package com.scqkxx.gamebox.activity.group;

import android.content.ContentValues;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.databinding.ActivityGroupDetailBinding;
import com.scqkxx.gamebox.databinding.ActivityGroupEditBinding;
import com.scqkzqtz.base.base.BaseActivity;

import java.util.List;

public class GroupDetailActivity extends BaseActivity<ActivityGroupDetailBinding> {

    private String groupId;

    @Override
    public int setLayoutId() {
        return R.layout.activity_group_detail;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(groupId == null){
            groupId = getIntent().getStringExtra("groupId");
        }

        AVQuery<AVObject> query = AVQuery.getQuery("Group");
        query.whereEqualTo("objectId",Integer.parseInt(groupId));
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                Log.i("Group",avObjects+"");
                if (e == null && avObjects != null && avObjects.size() > 0) {
                    for (int i = 0; i <avObjects.size() ; i++) {
                        if (avObjects.get(i).getObjectId().equals(groupId)){
                            AVObject avObject = avObjects.get(i);
                            if (Integer.valueOf(AVUser.getCurrentUser().getObjectId()) == avObject.getInt("belongUserId")){
                                //自己的群组
                                binding.tvGroup.setText("群组成员");
                                binding.tvSee.setText("查看");
                                binding.linNewsPerson.setVisibility(View.VISIBLE);
                            } else {
                                //别人的群组
                                binding.tvGroup.setText("邀请加入");
                                binding.tvSee.setText("邀请");
                                binding.linNewsPerson.setVisibility(View.GONE);
                            }
                            binding.tvGroupName.setText(avObject.getString("name"));
                            binding.tvGroupContent.setText(avObject.getString("desc"));
                            binding.tvAscription.setText(avObject.getString("belongUserName"));
                            binding.cbSetDefault.setChecked(avObject.getInt("isPublic") == 0);
                            RequestOptions options = new RequestOptions()
                                    .placeholder(R.mipmap.icon_user_header_default)
                                    .error(R.mipmap.icon_user_header_default)
                                    .centerCrop()
                                    .circleCrop();
                            Glide.with(GroupDetailActivity.this)
                                    .load(avObject.getString("avatar"))
                                    .apply(options)
                                    .into(binding.ivGroupImg);
                            break;
                        }
                    }
                }
            }
        });
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener(mTitleType -> finish());

        groupId = getIntent().getStringExtra("groupId");
        binding.btEditGroup.setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("groupId",groupId);
            ActivityUtil.switchTo(this,GroupEditActivity.class,contentValues);
        });

        binding.linAdd.setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("groupId",groupId);
            ActivityUtil.switchTo(GroupDetailActivity.this,GroupMembersActivity.class,contentValues);
        });
        binding.linNewsPerson.setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("groupId",groupId);
            ActivityUtil.switchTo(GroupDetailActivity.this,GroupApplyListActivity.class,contentValues);
        });
    }
}
